package presstrack
{
	import flash.net.Responder;
	
	import mx.collections.ArrayCollection;
	import mx.utils.ObjectUtil;
	
	import org.presstrack.service.*;
	
	[RemoteClass(alias="presstrack.PaperIssueClass")]
	[Bindable]
	public class PaperIssueClass
	{
		// static
		public static var ISSUE_YEAR:Number					= new Date().getFullYear();
		public static var ISSUE_MONTH:Number				= new Date().getMonth() + 1;
		
		public static var ISSUE_START_DATE:String			= new String();
		public static var ISSUE_END_DATE:String				= new String();
		
		public static var ISSUE_REQUEST:int					= new int(-1);
		public static var ISSUE_LABEL:String				= new String();
		
		public static var ISSUE_MANAGERS:ArrayCollection	= new ArrayCollection();
		
		// public		
		public var iIssueId:int;
		public var mLabel:String;
		public var mCreationDate:String;
		
		public var iPaperId:int;
		public var mPaperLabel:String; 
		public var iPaperThick:int; 
		public var mPaperSize:String;
		
		public var mCustomer:String;
		
		public var iNum:int;
		public var mCost:String;
		
		public var iRequest:int;
		 
		public function PaperIssueClass(ob:Object = null):void
		{
			iIssueId 		= -1;
			mLabel 			= "?"; 
			mCreationDate 	= "??/??/????";			
			iPaperId 		= -1;
			mPaperLabel 	= "?"; 
			iPaperThick 	= 0; 
			mPaperSize 		= "?";			
			mCustomer 		= "";		
			iNum 			= 0; 
			mCost 			= "0.00";
			iRequest		= -1;
		
			if (ob != null)
			{
				copyFrom(ob);
			}
		}
		
		public function copyFrom(ob:Object):void
		{
			if (ob.hasOwnProperty("idissue"))
			{
				iIssueId = ob.idissue;		
			}		
			if (ob.hasOwnProperty("label"))
			{
				mLabel = ob.label.toString();
			}				
			if (ob.hasOwnProperty("creation"))
			{
				mCreationDate = ob.creation.toString();
			}			
			if (ob.hasOwnProperty("paper"))
			{
				iPaperId = ob.paper;	
			}
			if (ob.hasOwnProperty("paper_label"))
			{
				mPaperLabel = ob.paper_label.toString();
			}
			if (ob.hasOwnProperty("paper_thick"))
			{
				iPaperThick = ob.paper_thick;
			}
			if (ob.hasOwnProperty("paper_size"))
			{
				mPaperSize = ob.paper_size.toString();
			}
			if (ob.hasOwnProperty("num"))
			{
				iNum = ob.num;
			}				
			if (ob.hasOwnProperty("cost"))
			{
				mCost = Number(ob.cost).toFixed(2);
			}				
			if (ob.hasOwnProperty("request"))
			{
				iRequest = ob.request;
			}
			if (ob.hasOwnProperty("customer"))
			{
				mCustomer = ob.customer;
			}
		}
		
		public function setLabel(value		:String, 
								 resFunc	:Function	= null, 
								 faultFunc	:Function	= null):void
		{
			updateParam("label", value, onRes, faultFunc);
			
			function onRes(res:String):void 
			{
				if (res == "successful") 
				{
					mLabel = value;
					if (resFunc != null)
					{
						resFunc();
					}
				} 
				else 
				{
					if (faultFunc != null) faultFunc(res);
				}
			}
		}
		
		public function setCreation(value		:String, 
									resFunc		:Function	= null, 
									faultFunc	:Function	= null):void
		{
			updateParam("creation", value, onRes, faultFunc);
			
			function onRes(res:String):void 
			{
				if (res == "successful") 
				{
					mCreationDate = value;
					if (resFunc != null) 
					{
						resFunc();
					}
				} 
				else 
				{
					if (faultFunc != null)
					{
						faultFunc(res);
					}
				}
			}
		}
		
		public function setPaper(value		:String, 
								 resFunc	:Function	= null, 
								 faultFunc	:Function	= null):void
		{
			updateParam("paper", value, onRes, faultFunc);
			
			function onRes(res:String):void 
			{
				if (res == "successful") 
				{
					iPaperId = int(value);
					CrudService.crud.getPapers();
					if (resFunc != null) 
					{
						resFunc();
					}
				} 
				else 
				{
					if (faultFunc != null)
					{
						faultFunc(res);
					}
				}
			}
		}
		
		public function setNum(value		:String, 
							   resFunc		:Function	= null, 
							   faultFunc	:Function	= null):void
		{
			updateParam("num", value, onRes, faultFunc);
			
			function onRes(res:String):void 
			{
				if (res == "successful") 
				{
					iNum = int(value);
					CrudService.crud.getPapers(resFunc, faultFunc);
				} 
				else 
				{
					if (faultFunc != null)
					{
						faultFunc(res);
					}
				}
			}
		}
		
		public function setCost(value		:String, 
								resFunc		:Function	= null, 
								faultFunc	:Function	= null):void
		{
			updateParam("cost", value, onRes, faultFunc);
			
			function onRes(res:String):void 
			{
				if (res == "successful") 
				{
					mCost = Number(value).toFixed(2);
					if (resFunc != null) 
					{
						resFunc();
					}
				} 
				else 
				{
					if (faultFunc != null)
					{
						faultFunc(res);
					}
				}
			}
		}
		
		public function setRequest(value		:String, 
								   resFunc		:Function	= null, 
								   faultFunc	:Function	= null):void
		{
			updateParam("request", value, onRes, faultFunc);
			
			function onRes(res:String):void 
			{
				if (res == "successful") 
				{
					iRequest = int(value);
					if (resFunc != null) 
					{
						resFunc();
					}
				} 
				else 
				{
					if (faultFunc != null)
					{
						faultFunc(res);
					}
				}
			}
		}
		
		public function updateParams(_idpaper	:int, 
									 _creation	:String, 
									 _cost		:String, 
									 _num		:int, 
									 resFunc	:Function	= null, 
									 faultFunc	:Function	= null):void 
		{
			CrudService.crud.srv.call("PressTrack.updateIssue", new Responder(onRes, onFault), iIssueId, _idpaper, _creation, _cost, _num);
			
			function onRes(res:String):void 
			{
				if (res == "successful") 
				{
					if (iPaperId != _idpaper)
					{
						CrudService.crud.getPapers(resFunc, faultFunc);
					}
					else
					{
						mCreationDate = _creation;
						mCost = _cost;
						iNum = _num;
						
						for each(var p:PaperClass in CrudService.arPaper)
						{
							if (p.iPaperId == iPaperId)
							{
								p.iTotal -= iNum;
								break;
							}
						}
						CrudService.arPaper.refresh();	
						if (resFunc != null) 
						{
							resFunc();
						}					
					}					
				} 
				else 
				{
					if (faultFunc != null)
					{
						faultFunc(res);
					}
				}
			}
									
			function onFault(e:Object) : void 
			{
				faultFunc(ObjectUtil.toString(e));
			}					
		}
		
		// private
		private function updateParam(param		:String, 
									 value		:String, 
									 onResult	:Function	= null, 
									 faultFunc	:Function	= null):void 
		{
			CrudService.crud.srv.call("PressTrack.UpdateIssueParam", new Responder(onResult, onFault), iIssueId, param, value);
									
			function onFault(e:Object):void 
			{
				faultFunc(ObjectUtil.toString(e));
			}			
		}
	}
}